package com.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.task.common.CustomException;
import com.task.entity.Category;
import com.task.entity.Dish;
import com.task.entity.Setmeal;
import com.task.mapper.CategoryMapper;
import com.task.service.CategoryService;
import com.task.service.DishService;
import com.task.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = (int) dishService.count(dishLambdaQueryWrapper);

       if (count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
       }

       LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
       setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
       int count2 = (int) setmealService.count(setmealLambdaQueryWrapper);
       if (count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
       }

       super.removeById(id);
    }
}
